package com.xxl.job.consume.utils;

import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class StringUtil {
	
	
	public static List<String> StringToArrayList(String str, String separator) {
      List<String> arr = new ArrayList<String>();
      if ((str == null) || (separator == null)) {
          return arr;
      }
      StringTokenizer st = new StringTokenizer(str, separator);
      while (st.hasMoreTokens()) {
          arr.add(st.nextToken());
      }
      return arr;
  }

    public static String addZeroForNum(String str, int strLength) {
        int strLen = str.length();
        StringBuffer sb = null;
        while (strLen < strLength) {
            sb = new StringBuffer();
            sb.append("0").append(str);// 左补0
            str = sb.toString();
            strLen = str.length();
        }
        return str;
    }

    public static String listToString(List<String> list){
        if(list==null){
            return null;
        }
        StringBuilder result = new StringBuilder();
        for(String string :list) {
            result.append(string);
        }
        return result.toString();
    }


    /**
     * 给定一个长字符串内容，返回一个数组
     *
     * @param content
     *            所有的内容
     * @param count
     *            需要每段截取的长度
     * @return 所有分段的数组list
     */
    public static List<String> getListFromContent(String content, int count) {

        List<String> list = new ArrayList<String>();
        // 获取String的总长度
        int contentLength = content.length();

        if (contentLength < count) {
            list.add(content);
        } else {

            int begin = 0;
            // 获取需要切割多少段
            int cutCount = contentLength / count;

            int cutCounts = contentLength % count;

            // 获取切割段的长度
            if (cutCounts != 0) {
                cutCount++;
            }

            for (int i = 1; i <= cutCount; i++) {
                String temp;
                // 不是最后一段
                if (i != cutCount) {

                    temp = content.substring(begin, count * i);

                } else {
                    temp = content.substring(begin, contentLength);
                }

                begin = count * i;
                list.add(temp);
            }

        }

        return list;
    }

    /**
     * 正数、负数和小数正则判断
     * @param number
     * @return
     */
    public static boolean checkNum(String number) {
        String rex="^(\\-|\\+)?\\d+(\\.\\d+)?$";
        Pattern p = Pattern.compile(rex);
        Matcher m = p.matcher(number);
        return m.find();
    }

}
